package org.gruposp2p.openid

class IdRequestConfirmationController {

	def index = {
			
		def user
		def principal = springSecurityService.principal
		if (principal) {
			user = UserDNI.get(principal.id)
		} else {
			redirect controller:register, action: index
			return
		}
		if (!user) {
			flash.message = messageSource.resolveCode("register.userNotFound", request.locale).format(params.id)
			redirect controller:register, action: index, id: params.id
			return
		}

		// if user want to change password. leave passwd field blank, passwd will not change.
		if (params.passwd && params.passwd.length() > 0
				&& params.repasswd && params.repasswd.length() > 0) {
			if (params.passwd == params.repasswd) {
				user.password = springSecurityService.encodePassword(params.passwd)
			} else {
				user.password = ''
				flash.message = messageSource.resolveCode("register.passwordNotMatch", request.locale)
				render view: 'edit', model: [user: user]
				return
			}
		}
		user.userRealName = params.userRealName
		person.email = params.email
		if (params.emailShow) {
			person.emailShow = true
		}
		else {
			person.emailShow = false
		}

		if (person.save()) {
			redirect action: show, id: person.id
		}
		else {
			render view: 'edit', model: [person: person]
		}
			
	 }
	
}
